//应收统计

interface IReceivableStatisticsViewModel extends IBaseViewModel<IReceivableStatisticsQueryTerms> { 
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<ReceivableStatisticsResponse>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 查询下的数据
     */
    RData:ReceivableStatisticsResponse;
    /**
     * 结算单位提示
     */
    searchSettle:(value:string)=>any;
    /**
     * 回调函数
     */
    settleBoxSelect: (selected:angucomplete<SettleListItemResponse>)=> void;
    /**
     * 去重
     */
    removeDupl:(arr:Array<any>)=>void;
    /**
     * 财务明细
     */
    receivableFinance:()=>void;
    /**
     * 收入明细单
     */
    receivableIncomeList:()=>void;
    /**
     * 未结账明细单
     */
    receivableAccountsList:()=>void;
    /**
     * 导出报表
     */
    excel:()=>void;
    /**
     * 事件
     */
     click:()=> void;
     /**
      * 查询条件下的信息数据
      */
     receivableData:ReceivableStatisticsResponse;
     /**
      * 列表高
      */
      heightA:number;
      /**
       * 输入字段时调用的回调函数
       */
      inputChanged:(str:string)=>void;
}
/**
 * 查询条件
 */
interface IReceivableStatisticsQueryTerms{
    /**
     * 结算单位
     */
    settleName:string;
    /**
     * 起始时间
     */
    startTime:string;
    /**
     * 结束时间
     */
    endTime:string
}

class IReceivableStatisticsController extends ListController<IReceivableStatisticsViewModel,IReceivableStatisticsQueryTerms>{
    gridApi: uiGrid.IGridApiOf<ReceivableStatisticsResponse>;
    /**
     * 单选
     */
    selectReceivableStatistics:ReceivableStatisticsResponse;

    constructor($scope:IReceivableStatisticsViewModel, private receivableService:IReceivableService, private $ngBootbox: BootboxService,
    private settleService:ISettleService,private authorizationService:IAuthorizationService,private $location:ng.ILocationService,private orderService:IorderService,
    private $state: angular.ui.IStateService,private exportService:IexportService,private commonService:ICommonService){
        super($scope);
        this.init();
    }

    init():void{
        super.init();
        this.selectReceivableStatistics=null;
        
        this.$scope.queryTerms = {
            settleName:"",
            startTime:"",
            endTime:""
        }

        this.$scope.inputChanged = (str)=>{
            this.$scope.queryTerms.settleName = str;
        }
        
        $('#openstarttime').click(function () {
            $('#starttime').datetimepicker('show');
        });
        $('#openendtime').click(function () {
            $('#endtime').datetimepicker('show');
        });

        this.paginationOptions= {
            skipSize:0,
            
            pageSize: 50,
            currentPage: 1,
            
        };

        this.$scope.gridOptions = {
            multiSelect:false,//单选
            enableSelectAll: false,//全选禁用
            paginationPageSizes: [50, 60, 70],
            useExternalPagination: true,
            useExternalSorting: true,
            enableFullRowSelection: true,
            enableColumnResizing: true,
            enableSorting: false, 
            columnDefs:[
                { displayName: "结算单位", field: 'settle', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: "已开票金额", field: 'alreadyInvoiceAmount', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '不含税金额', field: 'excludingTaxAmount', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '收入金额', field: 'incomeAmount', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '未收金额', field: 'uncollectedAmount', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '未结账金额', field: 'accountEnRouteAmount', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '总余额', field: 'allBalance', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                    gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                    gridApi.selection.on.rowSelectionChanged(this.$scope, this.selectChange);
                }     
        };

        /**
         * 查询下的禁用数据
         */
        // this.receivableService.statistics("", $("#starttime").val(),  $("#endtime").val()).then((res)=>{
        //     this.$scope.RData = res.data;
        // });

        this.query();

        //结算单位提示
        this.$scope.searchSettle = this.searchSettle;
        this.$scope.settleBoxSelect = this.settleBoxSelect;

        this.$scope.receivableFinance = this.receivableFinance;
        this.$scope.receivableIncomeList = this.receivableIncomeList;
        this.$scope.receivableAccountsList = this.receivableAccountsList;
   
    }

    localHistory():void{
        super.localHistory(this.$state)
    }
    getHistory():void{
        super.getHistory(this.$state,this.$location)
    }

    

    /**
     * 加载数据
     */
    loadData(queryTerms:IReceivableStatisticsQueryTerms,skip:number,count:number):void{
        this.receivableService.statisticslist(queryTerms.settleName,queryTerms.startTime,queryTerms.endTime,skip,count).then((result)=>{
            this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;
                window.localStorage.setItem('totalStatisticslist',JSON.stringify(result.data.total));

                this.$scope.heightA = (this.$scope.gridOptions.data.length * 39)+95;

                var currentPage = this.paginationOptions.currentPage;
                var totalPages = this.gridApi.pagination.getTotalPages();
                if (totalPages < currentPage) { this.$scope.gridOptions.paginationCurrentPage = 1; };
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            }); 
            this.click();
    }

    /**
     * 查询条件下的信息
     */
    click=()=>{
        this.receivableService.statistics("",$("#starttime").val(), $("#endtime").val()).then((res)=>{
            this.$scope.receivableData = res.data;
        });
    }



    /**
     * 结算单位提示
     */
    searchSettle=(value:string)=>{
        return this.settleService.getList("",value,"",true,0,5).then((res)=>{
            return this.commonService.unique(res.data.data,'name');

        })
    }

    /**
     * 回调函数
     */
    settleBoxSelect=(selected:angucomplete<SettleListItemResponse>)=>{
        if(selected){
            // this.$scope.RData.settle = selected.description.id;
            this.$scope.receivableData.settle = selected.description.id;
            this.$scope.queryTerms.settleName = selected.description.name;
        }
    }

    /**
     * 单选
     */
     selectChange =(row:uiGrid.IGridRowOf<ReceivableStatisticsResponse>)=> {
        if (row.isSelected) {
            this.selectReceivableStatistics = row.entity;
        } else {
            this.selectReceivableStatistics = null;       
        }
    };

    /**
     * 财务明细
     */
    receivableFinance = ()=> {
        if(this.selectReceivableStatistics){
            this.$state.go('app.receivable.finance_excel',{settle:this.selectReceivableStatistics.settle,startTime:$('#starttime').val(),endTime:$('#endtime').val() });
        }else{
            this.$ngBootbox.alert('请选择一项');
            return;
        }
    }

    /**
     * 收入明细单
     */
    receivableIncomeList = ()=> {
        if(!this.selectReceivableStatistics){
            this.$ngBootbox.alert('请选择一项');
            return;
        };
        this.$state.go('app.receivable.income_excel',{settle:this.selectReceivableStatistics.settle,startTime:$('#starttime').val(),endTime:$('#endtime').val() });
    }

    /**
     * 未结账明细单
     */
    receivableAccountsList = ()=> {
        if(this.selectReceivableStatistics){
            this.$state.go('app.receivable.arrearage_excel',{settle:this.selectReceivableStatistics.settle,startTime:$('#starttime').val(),endTime:$('#endtime').val() });
        }else{
            this.$ngBootbox.alert('请选择一项');
            return;
        }
    }

    /**
     * 导出报表
     */
    exportFunc=()=>{
        if(this.$scope.gridOptions.totalItems>60000){
            this.$ngBootbox.alert('导出数据不可超过6万条');
            return;
        }else{
            this.exportService.exportstatistics(this.queryTerms.settleName,this.$scope.queryTerms.startTime,this.$scope.queryTerms.endTime);
        }
        
    }


}

angular.module("tms").controller("receivableStatisticsController", ["$scope", "receivableService", "$ngBootbox" , "settleService", "authorizationService", "$location", "orderService",
 "$state","exportService","commonService", IReceivableStatisticsController]);